/**
 * 页面初始化公用方法
 */
var layer, laydate, AES_KEY;

$(function() {
    layer = layui.layer, laydate = layui.laydate;
    AES_KEY = '8NONwyJtHesysWpM';

    // select2事件绑定
    if ($.fn.select2 !== undefined) {
        $.fn.select2.defaults.set( 'language', language);
        $.fn.select2.defaults.set( 'theme', 'classic');
    }
    $('select.form-control.select2').each(function () {
        $(this).select2();
    });

    // checkbox 事件绑定
    if ($('.check-box').length > 0) {
        $('.check-box').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue'
        });
    }

    // radio 事件绑定
    if ($('.radio-box').length > 0) {
        $('.radio-box').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue'
        });
    }

    if ($('.dropdown-toggle').length > 0) {
        $('.dropdown-toggle').dropdownHover();
    }

    // laydate 时间控件绑定
    if ($('.time-input').length > 0) {
        $('.time-input').each(function() {
            var auto = $(this).attr('render');
            if (auto) {
                var id = $(this).attr('id');
                laydate.render({
                    elem: '#' + id,
                    theme: '#1E9FFF',
                    calendar: true
                });
            }
        });
    }

    $('.modal').appendTo('body'),
        $('[data-toggle=popover]').popover(),
        $('.collapse-link').click(function() {
        var div_ibox = $(this).closest('div.ibox'),
            e = $(this).find('i'),
            i = div_ibox.find('div.ibox-content');
            i.slideToggle(200),
            e.toggleClass('fa-chevron-up').toggleClass('fa-chevron-down'),
            div_ibox.toggleClass('').toggleClass('border-bottom'),
            setTimeout(function() {
                div_ibox.resize();
            }, 50);
    }), $('.close-link').click(function () {
        var div_ibox = $(this).closest('div.ibox');
        div_ibox.remove()
    });
});

function Encrypt(content){
    var key = CryptoJS.enc.Utf8.parse(AES_KEY);
    var srcs = CryptoJS.enc.Utf8.parse(content);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {
        mode:CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
}

function Decrypt(content){
    var key = CryptoJS.enc.Utf8.parse(AES_KEY);
    var decrypt = CryptoJS.AES.decrypt(content, key, {
        mode:CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}